package vsm.vsm_server.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.VideoMapper;
import vsm.vsm_server.pojo.User.Record.History.HistoryVideo;
import vsm.vsm_server.pojo.User.Record.Like.LikeVideo;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.pojo.Video.*;
import vsm.vsm_server.service.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class VideoServiceImpl implements VideoService {

    private VideoMapper videoMapper;
    @Autowired
    private VodService vodService;
    @Autowired
    private UserService userService;
    @Autowired
    private RelationService relationService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RecService recService;
    @Autowired
    private HistoryService historyService;
    public VideoServiceImpl(VideoMapper videoMapper) {this.videoMapper = videoMapper;}

    @Override
    public BigInteger getUserId(BigInteger videoId) {
        return videoMapper.getUserId(videoId);
    }

    @Override
    public List<VideoNailVO> getSelfVideos(BigInteger userid) {
        List<Video> videoList = videoMapper.getSelfVideos(userid);
        List<VideoNailVO> videoNailVOList = new ArrayList<>();
        if (videoList != null) {
            for (Video v : videoList) {
                VideoNailVO newVideoNailVO = new VideoNailVO();
                BeanUtils.copyProperties(v, newVideoNailVO);
                VideoUrlVO newVideoUrlVO = vodService.getUrl(newVideoNailVO.getPath());
                BeanUtils.copyProperties(newVideoUrlVO, newVideoNailVO);
                videoNailVOList.add(newVideoNailVO);
            }
        }
        return videoNailVOList;


    }

    @Override
    public List<BigInteger> getUpVideoIds(BigInteger userid) {
        return videoMapper.getUpVideoIds(userid);
    }

/*
    @Override
    public VideoRecVO videoToRecVideo(Video v, BigInteger userid) {
        VideoRecVO videoRecVO = new VideoRecVO();
        BeanUtils.copyProperties(v, videoRecVO);
        String avatar = userService.getAvatarId(videoRecVO.getUserId());
        if (avatar != null)
            avatar = vodService.getPicture(avatar);
        videoRecVO.setAvatar(avatar);
        if (userid.compareTo(BigInteger.valueOf(-1)) != 0) {
            Integer relation = relationService.isFollow(userid, videoRecVO.getUserId());
            if (Objects.equals(userid, videoRecVO.getUserId())) {
                videoRecVO.setRelation(-1);
            } else {
                videoRecVO.setRelation(Objects.requireNonNullElse(relation, 0));
            }
            LikeVideo likeVideo = new LikeVideo();
            likeVideo.setUserId(userid);
            likeVideo.setVideoId(videoRecVO.getVideoId());
            Boolean islike = likeService.isLikeVideo(likeVideo);

            videoRecVO.setIsLike(islike);
        } else { //未登录时userid为-1
            videoRecVO.setRelation(0);
            videoRecVO.setIsLike(false);
        }
        return videoRecVO;
    }

*/

    @Override
    public void deleteVideo(BigInteger videoId) {
        videoMapper.deleteById(videoId);
    }

    @Override
    public void createVideo(Video newVideo) {

        videoMapper.create(newVideo);
    }

    @Override
    public Video getVideo(BigInteger videoId) {
        return videoMapper.getById(videoId);
    }


    //上传视频
    @Override
    public Boolean updateVideo(Video newVideo) {

        return videoMapper.update(newVideo);
    }

    @Override
    public void addLikeCount(BigInteger videoId) {
        videoMapper.addLikeCount(videoId);
    }

    @Override
    public void deLikeCount(BigInteger videoId) {
        videoMapper.deLikeCount(videoId);
    }


    @Override
    public List<VideoIdVO> getRecVideoList(BigInteger userid, Integer videocount) {
        List<VideoIdVO> videoList = new ArrayList<>();
        //List<BigInteger> videoIdList = recService.getRandomVideoList(userid,videocount);
        List<BigInteger> videoIdList = null;
        try {
            videoIdList = recService.GetRedisVideoList(userid,videocount);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        for(BigInteger i:videoIdList){
            VideoIdVO v = new VideoIdVO(i);
            videoList.add(v);
        }
        return videoList;
    }

    @Override
    public VideoDetailVO videoToDetailVideo(Video video, BigInteger userid) {
        VideoDetailVO videoDetailVO = new VideoDetailVO();
        BeanUtils.copyProperties(video, videoDetailVO );
        //获取总评论数
        Double commentCount =commentService.getCommentCount(video.getVideoId());
        videoDetailVO.setCommentCount(commentCount);
        //获取视频上传者信息
        UserInfo userInfo = userService.getInfoById(videoDetailVO.getUserId());
        //获取用户名
        videoDetailVO.setUserName(userInfo.getUserName());
        //获取头像path
        String avatar = userInfo.getAvatar();
        if (avatar != null)
            //获取头像url
            avatar = vodService.getPicture(avatar);
        videoDetailVO.setAvatar(avatar);
        //获取视频上传者与用户关系
        if (userid.compareTo(BigInteger.valueOf(-1)) != 0) {
            Integer relation = relationService.isFollow(userid, videoDetailVO.getUserId());
            if (Objects.equals(userid, videoDetailVO.getUserId())) {
                videoDetailVO.setRelation(-1);
            } else {
                videoDetailVO.setRelation(Objects.requireNonNullElse(relation, 0));
            }
            LikeVideo likeVideo = new LikeVideo();
            likeVideo.setUserId(userid);
            likeVideo.setVideoId(videoDetailVO.getVideoId());
            Boolean islike = likeService.isLikeVideo(likeVideo);

            videoDetailVO.setIsLike(islike);
            //获取历史记录
            HistoryVideo historyVideo = historyService.getHistory(videoDetailVO.getUserId(),videoDetailVO.getVideoId());
            if(historyVideo!=null){
                videoDetailVO.setWatchTime(historyVideo.getCreateTime());
                videoDetailVO.setCurrentTime(historyVideo.getCurrentTime());
            }else{

                videoDetailVO.setCurrentTime(Double.valueOf(0));
            }
        } else { //未登录时userid为-1
            videoDetailVO.setRelation(0);
            videoDetailVO.setIsLike(false);

            videoDetailVO.setCurrentTime(Double.valueOf(0));
        }
        return videoDetailVO ;
    }

    @Override
    public List<VideoTag> getTagIds(List<BigInteger> videoId) {
        return videoMapper.getTagIds(videoId);
    }

    @Override
    public List<BigInteger> findTopNHotVideoIds(int count) {
        return videoMapper.findTopNHotVideoIds(count);
    }

    @Override
    public List<BigInteger> findRandomNewVideoIds(int count) {
        return videoMapper.getRandomVideos(count);
    }
}
